import Vue from 'vue'
import Router from "vue-router"
// import Login from "@/components/Login";
// 使用路由懒加载
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Login')
// import Home from "@/pages/Home"
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '@/pages/Home')

// import Welcome from "@/pages/Welcome";
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '@/pages/Welcome')

// import Users from "@/pages/user/Users";
// import Rights from "@/pages/power/Rights";
// import Roles from "@/pages/power/Roles";
const Users = () => import(/* webpackChunkName: "rights_roles_users" */ '@/pages/user/Users')
const Rights = () => import(/* webpackChunkName: "rights_roles_users" */ '@/pages/power/Rights')
const Roles = () => import(/* webpackChunkName: "rights_roles_users" */ '@/pages/power/Roles')


// import Cate from '@/pages/goods/Cate'
// import Params from "@/pages/goods/Params";
// import List from "@/pages/goods/List";
const Cate = () => import(/* webpackChunkName: "cate_params_list" */ '@/pages/goods/Cate')
const Params = () => import(/* webpackChunkName: "cate_params_list" */ '@/pages/goods/Params')
const List = () => import(/* webpackChunkName: "cate_params_list" */ '@/pages/goods/List')


// import Add from "@/pages/goods/Add";
// import Orders from "@/pages/orders/Orders";
// import Report from "@/pages/report/Report";
const Add = () => import(/* webpackChunkName: "add_orders_report" */ '@/pages/goods/Add')
const Report = () => import(/* webpackChunkName: "add_orders_report" */ '@/pages/report/Report')
const Orders = () => import(/* webpackChunkName: "add_orders_report" */ '@/pages/orders/Orders')



Vue.use(Router)

const router =  new Router({
  routes:[
    {
      name:'Login',
      path:'/login',
      component:Login
    },
    {
      name:'Home',
      path:'/home',
      component:Home,
      redirect:'/welcome',
      children:[
        {
          path:'/welcome',
          component:Welcome

        },
        {
          path:'/users',
          component:Users
        },
        {
          path:'/rights',
          component:Rights
        },
        {
          path:'/roles',
          component:Roles
        },
        {
          path:'/categories',
          component:Cate
        },
        {
          path:'/params',
          component:Params
        },
        {
          path:'/goods',
          component:List
        },
        {
          path:'/goods/add',
          component:Add
        },
        {
          path:'/orders',
          component:Orders
        },
        {
          path:'/reports',
          component:Report
        }
      ]
    },
    {

      path:'/',
      redirect:'/login'
    },
  ]
})

// 挂载路由导航守卫
router.beforeEach((to,from,next)=>{
//  to 将要访问的路径
//  from 代表从哪个路径跳转而来
//  next 是一个函数表示放行
//   next()放行 next('/login') 表示强制跳转
  if(to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr) return next('/login')
  next()
})
export default router
